<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>
        // function timeout(delay = 2000) {
        //     return new Promise((resolve, reject) => setTimeout(resolve, delay))
        // }
        // timeout().then(() => {
        //     console.log('2s后执行')
        //     return timeout(3000)
        // }).then(() => {
        //     console.log('再隔3s后执行')
        // })


        function interval(delay, callback) {
            return new Promise((resolve, reject) => {
                let timerId = setInterval(() => {
                    callback(timerId, resolve)

                }, delay)
            })
        }
        interval(3000, (timerId, resolve) => {
            console.log('3s后执行')
            clearInterval(timerId)
            resolve()
        }).then(() => {
           return interval(2000, (timerId, resolve) => { //注意 如果这里不写return .then()回调函数 相当于没有返回值，会导致最后一行.then()和这行.then()里面的回调函数同步执行
                clearInterval(timerId)
                resolve()
            })
        }).then(() => {
            console.log('再隔2s后执行')
        })
    </script>
</body>

</html>